/**
 * 
 *   Code contributed by IUP Research Institute
 * 
 *     This file is part of NetworkMon.
 *
 *   NetworkMon is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 of the License.
 *
 *   NetworkMon is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with NetworkMon; if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *   
 *   @author Brian Slocum
 */
package networkgui;

import java.awt.GridLayout;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import networkbackend.AddressLinkedList;
import networkbackend.AddressNode;

public class ComputerInformation extends javax.swing.JPanel {
	JLabel computerAlias;
	JLabel computerIP;
	JLabel totalSent;
	JLabel totalRecived;
	JPanel thePacketPanel = new JPanel();
	Box theComputerInformationPanel = Box.createVerticalBox();
	// JPanel theComputerInformationPanel = new JPanel();
	Box theMasterContainer = Box.createVerticalBox();

	JTable packetViewer;
	AddressNode theComputer = new AddressNode();
	PacketTableModel packetModel;

	// public new
	public ComputerInformation() {
		// init controls
		initControls();
	}

	private void initControls() {
		// init all fields
		// computerAlias = new JLabel("Computer Alis: MY NAME");
		computerIP = new JLabel();
		totalSent = new JLabel();
		totalRecived = new JLabel();
		packetModel = new PacketTableModel();

		// set up the table
		packetViewer = new JTable(packetModel);

		// set up the UI
		computerIP.setText("IP: " + theComputer.address);
		totalSent.setText("Total Sent: " + theComputer.totalSentLifetimeBytes);
		totalRecived.setText("Total Received: "
				+ theComputer.totalReceivedLifetimeBytes);

		// Add stuffs to the computer info panel
		theComputerInformationPanel.add(computerIP);
		theComputerInformationPanel.add(totalSent);
		theComputerInformationPanel.add(totalRecived);

		// set up the layout for the packets
		thePacketPanel.add(new JScrollPane(packetViewer)); // this right here
															// is bull sh*t...
															// it is the only
															// way to the the
															// frecken headers
															// to show up
		thePacketPanel.setLayout(new GridLayout(0, 1));

		// add everything to the master layout panel
		theMasterContainer.add(theComputerInformationPanel);
		theMasterContainer.add(thePacketPanel);

		// add to the frame
		setLayout(new GridLayout(0, 1));
		add(theMasterContainer);
	}

	public void setPacketInformation(AddressLinkedList thePacketMasterList) {
		packetModel.setInformation(thePacketMasterList);

	}

	public static class PacketTableModel extends AbstractTableModel {

		AddressLinkedList myPacketlist = new AddressLinkedList();

		private String[] theHeadings = new String[] { "Address", "Port",
				"Total Received Size", "Total Sent Size",
				"Total Recived Lifetime Bytes", "Something" };

		public int getRowCount() {
			return myPacketlist.size();
		}

		public int getColumnCount() {
			return 5;
		}

		@Override
		public String getColumnName(int column) {
			return theHeadings[column];
		}

		public Object getValueAt(int row, int column) {
			/*
			 * col 0 = Packet Address col 1 = port col 2 = size col 3 =
			 * protocall col 4 = time
			 */

			switch (column) {
			case 0:
				return ((AddressNode) myPacketlist.get(row)).address;
			case 1:
				return ((AddressNode) myPacketlist.get(row)).port;
			case 2:
				return ((AddressNode) myPacketlist.get(row)).totalReceivedNumberOfBytes;
			case 3:
				return ((AddressNode) myPacketlist.get(row)).totalSentLifetimeBytes;
			case 4:
				return ((AddressNode) myPacketlist.get(row)).totalReceivedLifetimeBytes;
			default:
				return ((AddressNode) myPacketlist.get(row)).address;
			}
		}

		public void setInformation(AddressLinkedList ThePacketList) {
			myPacketlist = ThePacketList;
			fireTableDataChanged();

		}
	}
}
